﻿using System;
using Microsoft.Xna.Framework;

namespace ZombieGame
{
    public class VectorRectangle
    {
        public Vector2 TopLeft = new Vector2();
        public Vector2 BottomRight = new Vector2();
        public Vector2 Size { get { return BottomRight - TopLeft; } set { BottomRight = TopLeft + value; } }
        public Vector2 Position { get { return TopLeft; } set { TopLeft = value; } }
        public float Top { get { return TopLeft.Y; } set { TopLeft.Y = value; } }
        public float Left { get { return TopLeft.X; } set { TopLeft.X = value; } }
        public float Bottom { get { return BottomRight.Y; } set { BottomRight.Y = value; } }
        public float Right { get { return BottomRight.X; } set { BottomRight.X = value; } }
        public float X { get { return Left; } set { Left = value; } }
        public float Y { get { return Top; } set { Top = value; } }

        public static bool Collide(VectorRectangle r1, VectorRectangle r2)
        {
            bool[] f = new bool[8];
            f[0] = r1.Top < r2.Bottom;
            f[1] = f[0] && r1.Top > r2.Top;
            f[2] = r1.Left < r2.Right;
            f[3] = f[2] && r1.Left > r2.Left;
            f[4] = r1.Bottom > r2.Top;
            f[5] = f[4] && r1.Bottom < r2.Bottom;
            f[6] = r1.Right > r2.Left;
            f[7] = f[6] && r1.Right < r2.Right;
            if (f[1] && f[2] && f[6])
                return true;
            if (f[3] && f[0] && f[4])
                return true;
            if (f[5] && f[2] && f[6])
                return true;
            if (f[7] && f[0] && f[2])
                return true;
            return false;
        }
        public bool Collide(VectorRectangle r)
        {
            return Collide(this, r);
        }

        public static bool Contact(VectorRectangle r1, VectorRectangle r2)
        {
            bool[] f = new bool[8];
            f[0] = r1.Top == r2.Bottom || r1.Top == r2.Top;
            f[1] = r1.Top > r2.Top;
            f[2] = r1.Left == r2.Right || r1.Left == r2.Left;
            f[3] = r1.Left > r2.Left;
            f[4] = r1.Bottom == r2.Top || r1.Bottom == r2.Bottom;
            f[5] = r1.Bottom < r2.Bottom;
            f[6] = r1.Right == r2.Left || r1.Right == r2.Right;
            f[7] = r1.Right < r2.Right;
            if (f[1] && f[2] && f[6])
                return true;
            if (f[3] && f[0] && f[4])
                return true;
            if (f[5] && f[2] && f[6])
                return true;
            if (f[7] && f[0] && f[2])
                return true;
            return false;
        }


        public static VectorRectangle CollideOverlap(VectorRectangle r1, VectorRectangle r2)
        {
            VectorRectangle ret = VectorRectangle.Empty;
            if (Collide(r1, r2))
            {
                ret.X = (float)Math.Max(r1.X, r2.X);
                ret.Y = (float)Math.Max(r1.Y, r2.Y);
                ret.Right = (float)Math.Min(r1.Right, r2.Right);
                ret.Bottom = (float)Math.Min(r1.Bottom, r2.Bottom);
            }
            return ret;
        }

        public VectorRectangle CollideOverlap(VectorRectangle r)
        {
            return CollideOverlap(this, r);
        }

        public bool Contact(VectorRectangle r)
        {
            return Contact(this, r);
        }

        public static bool CollideOrContact(VectorRectangle r1, VectorRectangle r2)
        {
            return Contact(r1, r2) || Collide(r1, r2);
        }

        public bool CollideOrContact(VectorRectangle r)
        {
            return CollideOrContact(this, r);
        }

        public VectorRectangle(Vector2 topLeft, Vector2 bottomRight)
        {
            TopLeft = topLeft;
            BottomRight = bottomRight;
        }

        public VectorRectangle(float left, float top, float right, float bottom)
        {
            Top = top;
            Left = left;
            Bottom = bottom;
            Right = right;
        }

        public static VectorRectangle Empty { get { return new VectorRectangle(0f, 0f, 0f, 0f); } }

        public static bool operator ==(VectorRectangle r1, VectorRectangle r2)
        {
            return r1.Equals(r2);
        }

        public static bool operator !=(VectorRectangle r1, VectorRectangle r2)
        {
            return !(r1 == r2);
        }

        public override bool Equals(object obj)
        {
            if (obj is VectorRectangle)
            {
                VectorRectangle r = ((VectorRectangle)obj);
                return this.TopLeft == r.TopLeft && this.BottomRight == r.BottomRight;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}

